package org.jeecg.modules.demo.pandian.service.impl;

import org.jeecg.modules.demo.pandian.entity.Pandian;
import org.jeecg.modules.demo.pandian.entity.Pandiandetail;
import org.jeecg.modules.demo.pandian.mapper.PandiandetailMapper;
import org.jeecg.modules.demo.pandian.mapper.PandianMapper;
import org.jeecg.modules.demo.pandian.service.IPandianService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 手动盘点
 * @Author: jeecg-boot
 * @Date:   2022-10-10
 * @Version: V1.0
 */
@Service
public class PandianServiceImpl extends ServiceImpl<PandianMapper, Pandian> implements IPandianService {

	@Autowired
	private PandianMapper pandianMapper;
	@Autowired
	private PandiandetailMapper pandiandetailMapper;
	
	@Override
	@Transactional
	public void saveMain(Pandian pandian, List<Pandiandetail> pandiandetailList) {
		pandianMapper.insert(pandian);
		if(pandiandetailList!=null && pandiandetailList.size()>0) {
			for(Pandiandetail entity:pandiandetailList) {
				//外键设置
				entity.setPid(pandian.getId());
				pandiandetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Pandian pandian,List<Pandiandetail> pandiandetailList) {
		pandianMapper.updateById(pandian);
		
		//1.先删除子表数据
		pandiandetailMapper.deleteByMainId(pandian.getId());
		
		//2.子表数据重新插入
		if(pandiandetailList!=null && pandiandetailList.size()>0) {
			for(Pandiandetail entity:pandiandetailList) {
				//外键设置
				entity.setPid(pandian.getId());
				pandiandetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		pandiandetailMapper.deleteByMainId(id);
		pandianMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			pandiandetailMapper.deleteByMainId(id.toString());
			pandianMapper.deleteById(id);
		}
	}
	
}
